﻿<%@ WebHandler Language="C#" Class="_common" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using System.Collections.Generic;
using NS.BLL.Systems;
using NS.Model.Systems;
using NS.Utilities;
using BLL;
using Model;

public class _common : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //取得处事类型
        string action = context.Request["ac"];

        switch (action)
        {
            case "communty": //获取小区列表
                communty(context);
                break;
            case "cartype": //获取车型品牌
                cartype(context);
                break;
            case "cartypelist": //获取车型品牌列表
                cartypelist(context);
                break;
            case "carlist": //获取车型
                carlist(context);
                break;
            case "cpscheme": //获取洗车券套餐
                cpscheme(context);
                break;
            case "area": //获取区域
                area(context);
                break;
            case "project": //获取服务项目
                project(context);
                break;
            case "checkuser": //验证用户
                checkuser(context);
                break;
            case "serve": //服务车型
                serve(context);
                break;
            case "getWechatConfig": //获取微信配置
                getWechatConfig(context);
                break;
        }
    }

    private void getWechatConfig(HttpContext context)
    {
        string result = string.Empty;
        var url = context.Request.QueryString["url"];
        string appId = "";
        string timestamp = "";
        string nonceStr = "";
        Wechat wcin = new Wechat();
        string signature = wcin.GetJSSignPublic(url, out appId, out timestamp, out nonceStr);
        if (!string.IsNullOrEmpty(signature))
        {
            result = "{'appId':'" + appId + "','nonceStr':'" + nonceStr + "','timestamp':'" + timestamp + "','signature':'" + signature + "'}";
        }
        context.Response.Write(result);
    }
    
    private void checkuser(HttpContext context)
    {
        string result = "No";
        if (CookieHelper.GetValue("openid") != null && !string.IsNullOrEmpty(CookieHelper.GetValue("openid")))
        {
            if (MemberService.IsRegister(CookieHelper.GetValue("openid")))
            {
                result = "True";
            }
            else
            {
                result = "False";
            }
        }
        context.Response.Write(result);
    }

    private void serve(HttpContext context)
    {
        string result = string.Empty;
        string title = context.Request["t"];
        Info m = InfoService.GetInfoByTitle(title);
        if (m != null)
        {
            result = m.Contents;
        }
        context.Response.Write(result);
    }
    private void communty(HttpContext context)
    {
        string result = string.Empty;
        List<Communty> list = CommuntyService.GetAllCommunty(null);
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }

    private void project(HttpContext context)
    {
        string result = string.Empty;
        List<WashPirce> list = WashPirceService.GetAllWashPirce();
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }

    private void cartype(HttpContext context)
    {
        string result = string.Empty;
        StringBuilder lejson = new StringBuilder();
        List<string> list = CarTypeService.GetCarTypeLetter();
        foreach (string le in list)
        {
            StringBuilder ctjson = new StringBuilder();
            List<CarType> ctList = CarTypeService.GetCarTypeByLetter(le);
            foreach (CarType ct in ctList)
            {
                ctjson.Append("{'id':'" + ct.TypeID + "','name':'" + ct.TypeName + "'},");
            }
            lejson.Append("{'le':'" + le + "','ct':[" + ctjson.ToString().Substring(0, ctjson.Length - 1) + "]},");
        }
        result = "[" + lejson.ToString().Substring(0, lejson.Length - 1) + "]";
        context.Response.Write(result);
    }

    private void cartypelist(HttpContext context)
    {
        string result = string.Empty;
        List<CarType> list = CarTypeService.GetAllCarTypeByParentID(0);
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }

    private void carlist(HttpContext context)
    {
        string result = string.Empty;
        long id = long.Parse(context.Request.QueryString["id"]);
        StringBuilder lejson = new StringBuilder();
        List<CarType> list = CarTypeService.GetAllCarTypeByParentID(id);
        foreach (CarType m in list)
        {
            StringBuilder ctjson = new StringBuilder();
            List<CarType> ctList = CarTypeService.GetAllCarTypeByParentID(m.TypeID);
            foreach (CarType ct in ctList)
            {
                ctjson.Append("{'id':'" + ct.TypeID + "','name':'" + ct.TypeName + "'},");
            }
            string ctstr = string.Empty;
            if (ctjson.Length > 0)
            {
                ctstr = ctjson.ToString().Substring(0, ctjson.Length - 1);
            }
            lejson.Append("{'ct':'" + m.TypeName + "','list':[" + ctstr + "]},");
        }
        if (lejson.Length > 0)
        {
            result = "[" + lejson.ToString().Substring(0, lejson.Length - 1) + "]";
        }
        context.Response.Write(result);
    }
    private void cpscheme(HttpContext context)
    {
        string result = string.Empty;
        List<CouponsScheme> list = CouponsSchemeService.GetAllCouponsScheme();
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }
    private void area(HttpContext context)
    {
        string result = string.Empty;
        string area = context.Request["area"];
        List<Area> list = AreaService.getAreaChildren(area);
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}